package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Product;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@WebServlet("/product/*")
public class ProductServlet extends BaseServlet {
    //查询数据，提供分页查询功能
    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传递的当前页码和每页显示的条目数
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        //调用业务层的方法遍历数据
        List<Product> list= productService.findAll(pageNum,pageSize);

        //将数据交给分页插件处理获取信息
        PageInfo<Product> pageInfo=new PageInfo<Product>(list);

        //将数据存储到指定的位置
        req.setAttribute("page",pageInfo);

        //请求转发到页面
        req.getRequestDispatcher("/pages/product-list.jsp").forward(req,resp);
    }

    //新建数据
    private void save(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //使用工具类获取传入的对象
        Product product = BeanUtil.fillBean(req, Product.class,"yyyy-MM-dd HH:mm");

        //调用业务层的方法操作数据
        productService.save(product);

        //操作完毕后显示全部数据
        findAll(req,resp);
    }

    //删除数据
    private void deleteByIds(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        //接收页面传回的数据
        String ids = req.getParameter("ids");
        String[] id = ids.split(",");

        //调用业务层的方法
        productService.deleteByIds(id);

        //操作完再用findAll遍历数据
        findAll(req,resp);
    }


    //修改数据，数据回显
    private void findById(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //获取数据
        String id = req.getParameter("id");

        //调用业务层的方法
        Product product=productService.findById(id);


        //将数据存储到作用域中
        req.setAttribute("product",product);

        //请求转发到页面
        req.getRequestDispatcher("/pages/product-update.jsp").forward(req,resp);
    }

    //修改数据
    private void update(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //使用工具类，将获取到的数据封装成一个对象
        Product product = BeanUtil.fillBean(req, Product.class,"yyyy-MM-dd HH:mm");
        String productStatus = req.getParameter("productStatus");
        System.out.println("productStatus = " + productStatus);
        //调用业务层的方法修改数据
        productService.update(product);

        //直接调用findAll方法遍历数据
        findAll(req,resp);
    }

    //开启状态按钮
    private void openByIds(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //从页面获取id值
        String ids= req.getParameter("ids");

        String[] id = ids.split(",");

        //调用业务层的方法处理
        productService.openByIds(id);

        //遍历数据
        findAll(req,resp);
    }

    //开启状态按钮
    private void closeByIds(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //从页面获取id值
        String ids= req.getParameter("ids");

        String[] id = ids.split(",");

        //调用业务层的方法处理
        productService.closeByIds(id);

        //遍历数据
        findAll(req,resp);
    }

    //导出数据文件功能
    private void download(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //生成报告的文件，然后传递到前端页面
        ByteArrayOutputStream os = productService.download();
        //获取产生响应的流对象
        ServletOutputStream sos = resp.getOutputStream();
        //将数据从原始的字节流对象中摄取出来写入到servlet对应的输出流中
        os.writeTo(sos);
        //刷新输出流
        sos.flush();
    }


}
